package vistas;

import helper.DateStringConverter;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import modelo.Mineral;
import modelo.vista.CerealCosechaFinaView;
import modelo.vista.CerealCosechaGruesaView;
import modelo.vista.CerealView;

import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.swingbinding.JListBinding;
import org.jdesktop.swingbinding.SwingBindings;

import controladores.AdministrateCeralesControlador;


public class AdministrateCeralesVista extends JPanel implements AbstractVista<AdministrateCeralesControlador> {

	private static final long serialVersionUID = 1L;
	private JTextField txtNombre;
	private JTextField txtRinde;
	private JTextField txtSiembraInicio;
	private JTextField txtDesperdicio;
	@SuppressWarnings("rawtypes")
	private JList listCereal;
	private JLabel lblNombre;
	private JLabel lblDesperdi;
	private JButton btnAltaCerealGruesa;
	@SuppressWarnings("rawtypes")
	private JComboBox cmbMineralPrincipal;
	private DateStringConverter dateHelper = new DateStringConverter();
	private JLabel lblEpocaSiembra;
	private JLabel lblRinde;
	private JButton btnBuscar;
	private JTextField txtBuscarCereal;
	private JButton btnModificarCereal;
	private JLabel lblBuscarCereal;
	private Mineral[] mineralesValues = Mineral.values();
	private JLabel lblError = new JLabel("");
	
	protected AdministrateCeralesControlador controlador;

	public AdministrateCeralesVista() {
		super();
		initComponents();
	}

	public AdministrateCeralesVista(AdministrateCeralesControlador controlador) {
		super();
		this.controlador = controlador;
		initComponents();
	}

	public AdministrateCeralesControlador getControlador() {
		return controlador;
	}

	public void setControlador(AdministrateCeralesControlador controlador) {
		this.controlador = controlador;
	}

	

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void initComponents() {
		this.setLayout(null);
		this.setPreferredSize(new java.awt.Dimension(754, 443));
		listCereal = new JList();
		listCereal.addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				String cerealNombre = (String) listCereal.getSelectedValue();
				CerealView c = controlador.buscarConNombre(cerealNombre);
				if (c instanceof CerealCosechaFinaView) {
					txtDesperdicio.setText("");
					txtDesperdicio.setVisible(false);
					lblDesperdi.setVisible(false);	
				}
				else if (c instanceof CerealCosechaGruesaView) {
					txtDesperdicio.setVisible(true);
					lblDesperdi.setVisible(true);
				}
			}
		});
		


		listCereal.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listCereal.setBounds(428, 164, 288, 66);
		add(listCereal);

		txtNombre = new JTextField();
		txtNombre.setBounds(176, 26, 219, 22);
		add(txtNombre);
		txtNombre.setColumns(10);

		txtRinde = new JTextField();
		txtRinde.setBounds(176, 96, 219, 22);
		add(txtRinde);
		txtRinde.setColumns(10);

		lblNombre = new JLabel("Nombre:");
		lblNombre.setBounds(27, 26, 97, 22);
		add(lblNombre);

		txtSiembraInicio = new JTextField();
		txtSiembraInicio.setColumns(10);
		txtSiembraInicio.setBounds(176, 61, 219, 22);
		add(txtSiembraInicio);


		JLabel lblMineralPrincipal = new JLabel("Mineral principal:");
		lblMineralPrincipal.setBounds(27, 167, 109, 16);
		add(lblMineralPrincipal);

		txtDesperdicio = new JTextField();
		txtDesperdicio.setColumns(10);
		txtDesperdicio.setBounds(139, 199, 256, 22);
		add(txtDesperdicio);

		JButton btnAltaCerealFina = new JButton(new ImageIcon(AdministrateCeralesVista.class.getResource("/uiImages/add.png")));
		btnAltaCerealFina.setHorizontalAlignment(SwingConstants.LEFT);
		btnAltaCerealFina.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				
				if(!txtNombre.getText().equals("") && !txtDesperdicio.getText().equals("") && !txtSiembraInicio.getText().equals("")){
					CerealCosechaFinaView c = new CerealCosechaFinaView();
					c.setNombre(txtNombre.getText());
					System.out.println(dateHelper.convertReverse(txtSiembraInicio.getText()));
					c.setEpocaSiembre(txtSiembraInicio.getText());
					c.setRinde(Float.parseFloat(txtRinde.getText()));
					c.setMineralPrincipal(mineralesValues[cmbMineralPrincipal.getSelectedIndex()]);
					controlador.altaCerealCocechaFina(c);
					txtNombre.requestFocusInWindow();
					txtNombre.selectAll();
					txtNombre.setText("");
					txtDesperdicio.setText("");
					txtSiembraInicio.setText("");
					}
				else{
					lblError.setVisible(true);
					lblError.setText("Debe completar todos los campos");
				}
			}
		});
		btnAltaCerealFina.setText("Agregar Cereal Fina");
		btnAltaCerealFina.setBounds(39, 257, 265, 43);
		add(btnAltaCerealFina);

		lblDesperdi = new JLabel("Desperdicio:");
		lblDesperdi.setBounds(27, 202, 79, 16);
		add(lblDesperdi);

		JButton btnBajaCereal = new JButton(new ImageIcon(AdministrateCeralesVista.class.getResource("/uiImages/remove.png")));
		btnBajaCereal.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				controlador.bajaCereal(controlador.buscarConNombre((String) listCereal.getSelectedValue()));
				controlador.getCerealesView().remove(0);
				txtDesperdicio.setText("");
				txtNombre.setText("");
				txtRinde.setText("");
				txtSiembraInicio.setText("");
			}
		});
		
		btnBajaCereal.setHorizontalAlignment(SwingConstants.LEFT);
		btnBajaCereal.setText("Remover Cereal");
		btnBajaCereal.setBounds(427, 297, 289, 42);
		add(btnBajaCereal);
		{
			lblEpocaSiembra = new JLabel();
			this.add(lblEpocaSiembra);
			lblEpocaSiembra.setText("Epoca de siembra:");
			lblEpocaSiembra.setBounds(27, 64, 131, 15);
		}
		{
			lblRinde = new JLabel();
			this.add(lblRinde);
			lblRinde.setText("Rinde:");
			lblRinde.setBounds(27, 99, 109, 15);
		}

		btnBuscar = new JButton();
		this.add(btnBuscar);
		btnBuscar.setText("Buscar");
		btnBuscar.setBounds(640, 242, 77, 23);
		btnBuscar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String nombre = txtBuscarCereal.getText();
				CerealView cerealBuscado = controlador.buscarConNombre(nombre);
				if(cerealBuscado != null){
					controlador.getCerealesView().add(cerealBuscado);
					listCereal.setSelectedIndex(0);
					txtBuscarCereal.setText("");
				}else{
					txtBuscarCereal.setText("No se encontro cereal");
				}
			}
		});

		lblBuscarCereal = new JLabel();
		this.add(lblBuscarCereal);
		lblBuscarCereal.setText("Buscar:");
		lblBuscarCereal.setBounds(428, 245, 46, 16);


		txtBuscarCereal = new JTextField();
		this.add(txtBuscarCereal);
		txtBuscarCereal.setBounds(479, 242, 155, 23);

		btnModificarCereal = new JButton();
		this.add(btnModificarCereal);
		btnModificarCereal.setText("Modificar cereal");
		btnModificarCereal.setBounds(427, 357, 290, 39);
		btnModificarCereal.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				controlador.modificarCereal(controlador.getCerealesView().get(0));
				controlador.getCerealesView().remove(0);
				txtDesperdicio.setText("");
				txtNombre.setText("");
				txtRinde.setText("");
				txtSiembraInicio.setText("");
			}
		});


		btnAltaCerealGruesa = new JButton(new ImageIcon(AdministrateCeralesVista.class.getResource("/uiImages/add.png")));
		
		btnAltaCerealGruesa.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				CerealCosechaGruesaView c = new CerealCosechaGruesaView();
				c.setNombre("nombre?");
				controlador.altaCerealCosechaGruesa(c);
				listCereal.setSelectedIndex(listCereal.getModel().getSize() - 1);
				txtNombre.requestFocusInWindow();
				txtNombre.selectAll();
			}
		});
		btnAltaCerealGruesa.setHorizontalAlignment(SwingConstants.LEFT);
		btnAltaCerealGruesa.setText("Agregar Cereal Gruesa");
		btnAltaCerealGruesa.setBounds(39, 312, 265, 45);
		add(btnAltaCerealGruesa);
		
		cmbMineralPrincipal = new JComboBox();
		cmbMineralPrincipal.setModel(new DefaultComboBoxModel(mineralesValues));
		cmbMineralPrincipal.setBounds(139, 164, 256, 22);
		add(cmbMineralPrincipal);
		lblError.setForeground(Color.RED);
		
		
		lblError.setVisible(false);
		lblError.setBounds(39, 406, 320, 37);
		add(lblError);
		
		txtDesperdicio.setVisible(false);
		lblDesperdi.setVisible(false);
		
		initDataBindings();
		
	}


	@SuppressWarnings("rawtypes")
	protected void initDataBindings() {
		BeanProperty<AdministrateCeralesControlador, List<CerealView>> mainControladorBeanProperty = BeanProperty.create("cerealesView");
		JListBinding<CerealView, AdministrateCeralesControlador, JList> jListBinding = SwingBindings.createJListBinding(UpdateStrategy.READ, controlador, mainControladorBeanProperty, listCereal);
		//
		BeanProperty<CerealView, String> cerealBeanProperty = BeanProperty.create("nombre");
		jListBinding.setDetailBinding(cerealBeanProperty);
		//
		jListBinding.bind();
		//
		BeanProperty<JList, Object> jListBeanProperty = BeanProperty.create("selectedElement.nombre");
		BeanProperty<JTextField, String> jTextFieldBeanProperty = BeanProperty.create("text");
		AutoBinding<JList, Object, JTextField, String> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, listCereal, jListBeanProperty, txtNombre, jTextFieldBeanProperty, "txtNombreBinding");
		autoBinding.bind();
		//
		BeanProperty<JList, Float> jListBeanProperty_1 = BeanProperty.create("selectedElement.rinde");
		BeanProperty<JTextField, String> jTextFieldBeanProperty_1 = BeanProperty.create("text");
		AutoBinding<JList, Float, JTextField, String> autoBinding_1 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, listCereal, jListBeanProperty_1, txtRinde, jTextFieldBeanProperty_1, "txtRindeBinding");
		autoBinding_1.bind();
		//
		BeanProperty<JList, String> jListBeanProperty_2 = BeanProperty.create("selectedElement.epocaSiembre");
		BeanProperty<JTextField, String> jTextFieldBeanProperty_2 = BeanProperty.create("text");
		AutoBinding<JList, String, JTextField, String> autoBinding_2 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, listCereal, jListBeanProperty_2, txtSiembraInicio, jTextFieldBeanProperty_2, "txtFechaInicioBinding");
		autoBinding_2.bind();
		//
		/*BeanProperty<JList, Date> jListBeanProperty_3 = BeanProperty.create("selectedElement.epocaSiembre.fechaFin");
		BeanProperty<JTextField, String> jTextFieldBeanProperty_3 = BeanProperty.create("text");
		AutoBinding<JList, Date, JTextField, String> autoBinding_3 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, listCereal, jListBeanProperty_3, txtsiembraFin, jTextFieldBeanProperty_3, "txtFechaFinBinding");
		autoBinding_3.setConverter(new DateStringConverter());
		autoBinding_3.bind();*/
		//
		BeanProperty<JList, Mineral> jListBeanProperty_5 = BeanProperty.create("selectedElement.porcentajeDesperdicio");
		BeanProperty<JTextField, String> jTextFieldBeanProperty_5 = BeanProperty.create("text");
		AutoBinding<JList, Mineral, JTextField, String> autoBinding_5 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, listCereal, jListBeanProperty_5, txtDesperdicio, jTextFieldBeanProperty_5, "txtDesperdicio");
		autoBinding_5.bind();
		//
		BeanProperty<JList, Mineral> jListBeanProperty_4 = BeanProperty.create("selectedElement.mineralPrincipalNecesario");
		BeanProperty<JComboBox, Object> jComboBoxBeanProperty = BeanProperty.create("selectedItem");
		AutoBinding<JList, Mineral, JComboBox, Object> autoBinding_4 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, listCereal, jListBeanProperty_4, cmbMineralPrincipal, jComboBoxBeanProperty);
		autoBinding_4.bind();
	}
}
